
#region Using Statements
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;

using Microsoft.Xna.Framework.Input;

using JigLibX.Collision;
using JigLibX.Physics;
using JigLibX.Geometry;
using JigLibX.Math;
using Microsoft.Xna.Framework.Graphics;
#endregion

namespace Game1
{
    public class Player
    {
        public int id;
        protected Vector3 speed;
        protected Matrix orientation;
        Camera camera;
        String name;
        public PhysicObject model;
        private Vector3 position;
        float i = 0;
        
        public Vector3 getPosition() { return model.PhysicsBody.Position; }
        public void setPosition(Vector3 position) { model.PhysicsBody.Position = position; }
    
        public Player(String name,PhysicObject model)
        {
            this.id = 0;
            this.name = name;
            this.model = model;
            camera = model.Camera;
            
            model.PhysicsSkin.SetMaterialProperties(0, new MaterialProperties(0.0f, 0, 0));
            PhysicsSystem.CurrentPhysicsSystem.AddController(new PlayerControl(model.PhysicsBody,this));            
        }

        public class PlayerControl : Controller 
        {
            Body body;
            Player player;
            public PlayerControl(Body body,Player player) 
            {
                this.body = body;
                this.player = player;
            }
            public override void UpdateController(float dt)
            {
                Vector3 force = Vector3.Zero;
                Vector3 torque = Vector3.Zero;


                if (Input.IsKeyDown(Keys.Left))
                    torque.Y += 8;
                if (Input.IsKeyDown(Keys.Right))
                    torque.Y -= 8;
                if (Input.IsKeyDown(Keys.Up))
                    torque.X = -4;
                if (Input.IsKeyDown(Keys.Down))
                    torque.X += 4;
                if (Input.IsKeyDown(Keys.D))
                    torque.Z -= 4;
                if (Input.IsKeyDown(Keys.A))
                    torque.Z += 4;
                //animation select

                if (Input.IsKeyDown(Keys.W))
                {
                    force.Z -= 8 * Config.Singleton.ShipSpeed;
                    AudioManager.playInfinitySound("engine_2");
                }
                else  AudioManager.pauseInfinitySound("engine_2");
                if (Input.IsKeyDown(Keys.S))
                    force.Z += 12 * Config.Singleton.ShipSpeed;
                if (force != Vector3.Zero)
                    body.AddBodyForce(force);
                body.AddBodyTorque(torque);

                body.Velocity = (1 - dt * Config.Singleton.SpeedResistance) * body.Velocity;
                body.AngularVelocity = (1 - dt * Config.Singleton.RotationResistance) * body.AngularVelocity;
            }
        }


        public void Update(GameTime time)
        {
            camera.Target = this.getPosition();
            Matrix rotationMatrix = model.PhysicsBody.Orientation;
            Vector3 transformedReference = Vector3.Transform(new Vector3(0, 1, Config.Singleton.CameraDistance), rotationMatrix);
            Vector3 target = this.getPosition() + transformedReference;
            Vector3 vel = 0.1f * (target - camera.getPosition());
            camera.setPosition(camera.getPosition() + vel);
            camera.Update(time, rotationMatrix);
            //camera.setPosition(target);           

            if (Input.IsKeyPressed(Keys.Space))
            {
               new Fire(model.PhysicsBody.Position, model.PhysicsBody.Orientation, camera, (float)Config.Singleton.ShotSpeed + model.PhysicsBody.Velocity.Length());
                //Vector3 vet = Vector3.UnitZ;
            
                  //vet = -Vector3.Transform(vet, model.PhysicsBody.Orientation);

               AudioManager.playSmallSound("tiro");
                 //Xna.Framework.Ray ray = new Xna.Framework.Ray(getPosition(), vet);
                 //ray.
            }
        }
      
    }
}
